﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.Business.Services;
using Gasco.DataAccess;

public partial class Controls_lookup_add : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null && !Page.IsPostBack)
        {
            LoadLookup(Convert.ToInt32(Request.QueryString["id"]));
        }
    }

    private void LoadLookup(int id) {
        string typeName = Request.QueryString["type"];

        if (String.IsNullOrEmpty(typeName))
            return;

        switch (typeName.ToLower())
        {
            case "country":
                LookupService<country> countryService = new LookupService<country>();
                country country = countryService.FindById(id);
                if (country != null)
                    tbName.Text = country.name;
                break;
            case "contact_type":
                LookupService<contact_type> contactTypeService = new LookupService<contact_type>();
                contact_type contactType = contactTypeService.FindById(id);
                if (contactType != null)
                    tbName.Text = contactType.name;
                break;
            case "order_type":
                LookupService<order_type> orderTypeService = new LookupService<order_type>();
                order_type orderType = orderTypeService.FindById(id);
                if (orderType != null)
                    tbName.Text = orderType.name;
                break;
            case "platts_type":
                LookupService<platts_type> plattsTypeService = new LookupService<platts_type>();
                platts_type plattsType = plattsTypeService.FindById(id);
                if (plattsType != null)
                    tbName.Text = plattsType.name;
                break;
            case "product_type":
                LookupService<product_type> productTypeService = new LookupService<product_type>();
                product_type p = productTypeService.FindById(id);
                if (p != null)
                    tbName.Text = p.name;
                break;
            case "unit":
                LookupService<unit> unitService = new LookupService<unit>();
                unit u = unitService.FindById(id);
                if (u != null)
                    tbName.Text = u.name;
                break;
            case "order_status":
                LookupService<order_status> orderStatus = new LookupService<order_status>();
                order_status os = orderStatus.FindById(id);
                if (os != null)
                    tbName.Text = os.name;
                break;
            case "currency":
                LookupService<currency> currencyService = new LookupService<currency>();
                currency c = currencyService.FindById(id);
                if (c != null)
                    tbName.Text = c.name;
                break;
            case "transportation_type":
                LookupService<transportation_type> transportService = new LookupService<transportation_type>();
                transportation_type t = transportService.FindById(id);
                if (t != null)
                    tbName.Text = t.name;
                break;
            case "border":
                LookupService<border> borderService = new LookupService<border>();
                border b = borderService.FindById(id);
                if (b != null)
                    tbName.Text = b.name;
                break;
           
            default:
                throw new Exception("Lookup type not found!");
        }
    }

    protected void btnclose_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
            AddLookup();
        else
            UpdateLookup();

        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script1", "<script>pageClose();</script>");
    }

   

    private void AddLookup()
    {

        string typeName = Request.QueryString["type"];
        string name = tbName.Text;

        if (String.IsNullOrEmpty(typeName))
            return;

        switch (typeName.ToLower())
        {
            case "country":
                LookupService<country> countryService = new LookupService<country>();
                countryService.Create(new country { name = name, create_date = DateTime.Now, update_date=DateTime.Now });
                break;
            case "contact_type":
                LookupService<contact_type> contactTypeService = new LookupService<contact_type>();
                contactTypeService.Create(new contact_type { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "order_type":
                LookupService<order_type> orderTypeService = new LookupService<order_type>();
                orderTypeService.Create(new order_type { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "platts_type":
                LookupService<platts_type> plattsTypeService = new LookupService<platts_type>();
                plattsTypeService.Create(new platts_type { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "product_type":
                LookupService<product_type> productTypeService = new LookupService<product_type>();
                productTypeService.Create(new product_type { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "unit":
                LookupService<unit> unitService = new LookupService<unit>();
                unitService.Create(new unit { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "order_status":
                LookupService<order_status> orderStatus = new LookupService<order_status>();
                orderStatus.Create(new order_status { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "currency":
                LookupService<currency> currencyService = new LookupService<currency>();
                currencyService.Create(new currency { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "transportation_type":
                LookupService<transportation_type> transportService = new LookupService<transportation_type>();
                transportService.Create(new transportation_type { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            case "border":
                LookupService<border> borderService = new LookupService<border>();
                borderService.Create(new border { name = name, create_date = DateTime.Now, update_date = DateTime.Now });
                break;
            default:
                throw new Exception("Lookup type not found!");
        }
    }

    private void UpdateLookup()
    {
        string typeName = Request.QueryString["type"];
        int id = Convert.ToInt32(Request.QueryString["id"]);
        string name = tbName.Text;

        if (String.IsNullOrEmpty(typeName))
            return;

        switch (typeName.ToLower())
        {
            case "country":
                LookupService<country> countryService = new LookupService<country>();
                country c = countryService.FindById(id);
                c.name = name;
                c.update_date = DateTime.Now;
                countryService.Update(c);
                break;
            case "contact_type":
                LookupService<contact_type> contactTypeService = new LookupService<contact_type>();
                contact_type ct = contactTypeService.FindById(id);
                ct.name = name;
                ct.update_date = DateTime.Now;
                contactTypeService.Update(ct);
                break;
            case "order_type":
                LookupService<order_type> orderTypeService = new LookupService<order_type>();
                order_type ot = orderTypeService.FindById(id);
                ot.name = name;
                ot.update_date = DateTime.Now;
                orderTypeService.Update(ot);
                break;
            case "platts_type":
                LookupService<platts_type> plattsTypeService = new LookupService<platts_type>();
                platts_type pt = plattsTypeService.FindById(id);
                pt.name = name;
                pt.update_date = DateTime.Now;
                plattsTypeService.Update(pt);
                break;
            case "product_type":
                LookupService<product_type> productTypeService = new LookupService<product_type>();
                product_type prt = productTypeService.FindById(id);
                prt.name = name;
                prt.update_date = DateTime.Now;
                productTypeService.Update(prt);
                break;
            case "unit":
                LookupService<unit> unitService = new LookupService<unit>();
                unit u = unitService.FindById(id);
                u.name = name;
                u.update_date = DateTime.Now;
                unitService.Update(u);
                break;
            case "order_status":
                LookupService<order_status> orderStatus = new LookupService<order_status>();
                order_status o = orderStatus.FindById(id);
                o.name = name;
                o.update_date = DateTime.Now;
                orderStatus.Update(o);
                break;
            case "currency":
                LookupService<currency> currencyService = new LookupService<currency>();
                currency cs = currencyService.FindById(id);
                cs.name = name;
                cs.update_date = DateTime.Now;
                currencyService.Update(cs);
                break;
            case "transportation_type":
                LookupService<transportation_type> transportService = new LookupService<transportation_type>();
                transportation_type ts = transportService.FindById(id);
                ts.name = name;
                ts.update_date = DateTime.Now;
                transportService.Update(ts);
                break;
            case "border":
                LookupService<border> borderService = new LookupService<border>();
                border b = borderService.FindById(id);
                b.name = name;
                b.update_date = DateTime.Now;
                borderService.Update(b);
                break;
            default:
                throw new Exception("Lookup type not found!");
        }
    }
}
